<template>
	<view class="layout">
		<input type="text" v-model="formData.name" placeholder="请输入姓名..."/>
		<input type="text" v-model="formData.age" placeholder="请输入年龄..."/>
		<button @click="handleAdd">点击</button>
	</view>
</template>

<script setup>
import { ref } from 'vue';
const formData = ref({
	name:"",
	age:""
})
const db = uniCloud.database();
const handleAdd = async () =>{
	uni.showLoading({
		title:"新增数据中。。。"
	})
	
	try{
		let res = await db.collection("demo-user").add(formData.value)
		
		console.log(res);
		
		if (res.result.errCode === 0) {
			console.log(res);
			uni.showToast({
				title:"新增成功"
			})
		}
	}catch(err){
		uni.showModal({
			content:err.errMsg || "新增失败",
			showCancel:false
		})
	}finally{
		uni.hideLoading()
	}
	
	/* db.collection("demo-user").add(
	{
		name:'小王',
		age:21,
		gender:1
	}).then(res=>{
		console.log(res)
		uni.showToast({
			title:"新增成功"
		})
	}).catch(err=>{
		uni.showModal({
			content:err.errMsg || "新增失败",
			showCancel:false
		})
	}).finally(()=>{
		uni.hideLoading()
	}) */
}

</script>

<style lang="scss" scoped>
.layout{
	padding: 30rpx;
	input{
		border:1px solid #eee;
		margin-bottom:30rpx;
		height: 80rpx;
		padding: 0 20rpx;
		border-radius: 10rpx;
	}
}   
</style>
